package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.Special_bulletinMapper;
import com.csr.csrcheck.pojo.Special_bulletin;
import com.csr.csrcheck.service.Special_bulletinService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class Special_bulletinServiceImpl implements Special_bulletinService {
    @Resource
    private Special_bulletinMapper special_bulletinMapper;

    public PageResult getlistpage(String release_time, String company_name, int pageNum, int pageSize) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageInfo(release_time, company_name, pageNum, pageSize));
    }

    public void addSpecial_bulletin(Special_bulletin special_bulletin) {
        int row = this.special_bulletinMapper.addSpecial_bulletin(special_bulletin);
        if (row != 1)
            throw new CompanyException("");
    }

    public int updateSpecial_bulletin(Special_bulletin special_bulletin) {
        int row = this.special_bulletinMapper.updateSpecial_bulletin(special_bulletin);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Special_bulletin findspecial_bulletinByid(int id) {
        Special_bulletin list = this.special_bulletinMapper.findspecial_bulletinByid(id);
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public int deletespecial_bulletinByid(int id) {
        int row = this.special_bulletinMapper.deletespecial_bulletinByid(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    private PageInfo<Special_bulletin> getPageInfo(String release_time, String company_name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Special_bulletin> list = this.special_bulletinMapper.getlistpage(release_time, company_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}
